<?php

class Inscription extends FrontEnd_Controller {

    function Inscription() {
        parent::FrontEnd_Controller();
        $this->_data['erreur'] = "";
        $this->load->model('Inscription_m');
    }

    function index() {
        $this->_data['page']="inscription";
        $this->_data['basket']=$this->Inscription_m->get_image_by_name('basket'); 
        $this->enregistrer();
    }
   function enregistrer() {
   
        $config = array(
            array(
                'field' => 'civile',
                'rules' => 'required'
            ),
            array(
                'field' => 'nom',
                'rules' => 'required'
            ),
            array(
                'field' => 'prenom',
                'rules' => 'required'
            ),
            array(
                'field' => 'profession',
                'rules' => 'required'
            ),
            array(
                'field' => 'statut',
                'rules' => 'required'
            ),
            array(
                'field' => 'raison_ent',
                'rules' => 'required'
            ),
            array(
                'field' => 'siret',
                'rules' => 'required|numeric'
            ),
            array(
                'field' => 'adr',
                'rules' => 'required'
            ),
            array(
                'field' => 'postale',
                'rules' => 'required|numeric|exact_length[5]'
            ),
            array(
                'field' => 'ville',
                'rules' => 'required'
            ),
            array(
                'field' => 'site',
                'rules' => 'url'
            ),
            array(
                'field' => 'tel',
                'rules' => 'required|numeric|min_length[10]'
            ),
            array(
                'field' => 'portable',
                'rules' => 'numeric|min_length[10]'
            ),
            array(
                'field' => 'email',
                'rules' => 'required|valid_email'
            ),
            array(
                'field' => 'sport[]',
                'rules' => 'required'
            ),
            array(
                'field' => 'recherche[]',
                'rules' => 'required'
            ),
            array(
                'field' => 'secteur',
                'rules' => 'required'
            ),
        );
        
        $this->_data['pratique'] = $this->input->post('pratique');
        $this->_data['date_creation'] = $this->input->post('date_creation');
        $this->_data['effectif'] = $this->input->post('effectif');
        $this->_data['precision'] = $this->input->post('precision');
        $this->_data['sports'] = $this->Inscription_m->get_sports();
        $this->_data['recherche'] = $this->Inscription_m->get_recherche();
        $this->_data['secteur'] = $this->Inscription_m->get_secteur();
        $this->form_validation->set_rules($config);
        
        if ($this->input->post('submit')) {
            $email=$this->input->post('email');
            $query = $this->Inscription_m->check_inscription($email);
           if ($query->num_rows() > 0) {
                    $this->_data['erreur'] = "Email existe !";
                    $this->_data['page']="inscription";
                     $this->_data['basket']=$this->Inscription_m->get_image_by_name('basket');
                    $this->display_view('home/inscription');
           } 
           else{ if ($this->form_validation->run()) {
                $image_path = './assets/img_web_ents/';
                $thumb_path=$image_path.'thumbs';
                $config['upload_path'] = $image_path;
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '2000';
                $config['max_width'] = '1024';
                $config['max_height'] = '768';
                $this->load->library('upload');
                 $this->load->library('image_lib');
                $this->upload->initialize($config);
                 $upload_info['full_path']="";
                 $upload_info1['full_path']="";
                  $upload_info['file_name']="";
                 $upload_info1['file_name']="";
                if($this->upload->do_upload('logo_ent')){
                 $upload_info = $this->upload->data();
                   $config = array(
                        'source_image' => $upload_info['full_path'],
                        'new_image' => $thumb_path,
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                    );
                    $this->image_lib->initialize($config);
                    $this->image_lib->resize();
                }
               

                 $image_path1 = './assets/img_web_ads/';
                 $thumb_path1 = $image_path1 . '/thumbs';
                $config['upload_path'] = $image_path1;
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '2000';
                $config['max_width'] = '1024';
                $config['max_height'] = '768';

          $this->upload->initialize($config);
          if($this->upload->do_upload('logo_ad')) {
             $upload_info1 = $this->upload->data();
                   $config = array(
                        'source_image' => $upload_info1['full_path'],
                        'new_image' => $thumb_path1,
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                    );
                    $this->image_lib->initialize($config);
                    $this->image_lib->resize();
          }
      
        $sport="";$recherche="";
         $asport=$this->input->post('sport');
         $N = count($asport);
         for($i=0; $i < $N; $i++)
         { if ($asport[$i]!="")$sport=$sport.','.$asport[$i];}  
         $sport = substr($sport, 1);
         
         $arecherche=$this->input->post('recherche');
         $N = count($arecherche);
         for($j=0; $j < $N; $j++)
         { if ($arecherche[$j]!="")$recherche=$recherche.','.$arecherche[$j];}
         $recherche = substr($recherche, 1);
         $data = array(
              
             'civille'=> $this->input->post('civile'),
             'nom'=> $this->input->post('nom'),
             'prenom'=> $this->input->post('prenom'),
             'profession'=> $this->input->post('profession'),
             'statut'=> $this->input->post('statut'),
             'raison_ent'=> $this->input->post('raison_ent'),
             'sport'=> $sport,
             'pratique'=> $this->input->post('pratique'),
             'precis'=> $this->input->post('precision'),
             'recherche'=> $recherche,
             'siret'=> $this->input->post('siret'),
             'date_creation'=> $this->input->post('date_creation'),
             'effectif'=> $this->input->post('effectif'),
             'adr'=> $this->input->post('adr'),
             'postale'=> $this->input->post('postale'),
             'ville'=> $this->input->post('ville'),
             'site'=> $this->input->post('site'),
             'tel'=> $this->input->post('tel'),
             'portable'=> $this->input->post('portable'),
             'email'=> $this->input->post('email'),
             'logo_ent'=> $upload_info['file_name'],
             'logo_ad'=>  $upload_info1['file_name'],
             'secteur'=> $this->input->post('secteur'),
             'ad'=> '0',
             'date_ins'=> time()
                    );
         $this->Inscription_m->add_inscription($data);

         $email= $this->input->post('email');
           $get_message= $this->Inscription_m->get_mail(1);
           mail($email, $get_message["titre"],$get_message["contenu"] , "From: contact@gwebcreation.com" );


         $this->_data['page']="inscription";
         $this->_data['basket']=$this->Inscription_m->get_image_by_name('basket');
         $this->display_view('home/success');                      
         } else{  
         $this->_data['page']="inscription";
         $this->_data['basket']=$this->Inscription_m->get_image_by_name('basket');
         $this->_data['erreur'] = "";
         $this->display_view('home/inscription');}
        }} else { 
        $this->_data['page']="inscription";
        $this->_data['erreur'] = "";
        $this->_data['basket']=$this->Inscription_m->get_image_by_name('basket');
        $this->display_view('home/inscription');}
       
    }

}

?>
